
#include "graphics.h"
#include "C_Application.h"
#include "MathUtil.h"
#include <list>
#include <algorithm>
#include <time.h>

C_Application* C_Application::s_application = NULL;

C_Application::C_Application(int screenWidth, int screenHeight)
: m_ScreenWidth(screenWidth)
, m_ScreenHeight(screenHeight)
, m_bInitialized(false)
{
    /* initialize random seed: */
    srand ( (unsigned int)time(NULL) );
} 

C_Application::~C_Application()
{

}

void C_Application::Tick(T_PressedKey pressedKeys, float fDeltaTime)
{
    //
    if(!m_bInitialized)
    {
        m_bInitialized = true;
        CreateAgents();
    }

    // update world
    unsigned int i = 0;
    for( i = 0; i < m_agentArray.size(); ++i)
    {
        m_agentArray[i]->Tick( fDeltaTime );
    }

    // clear screen on cannon position
    FillRect(0, 0, m_ScreenWidth, m_ScreenHeight, GetRGB(0, 0, 0));

    // render
    for( i = 0; i < m_agentArray.size(); ++i)
    {
        m_agentArray[i]->Render();
    }
}

void C_Application::CreateAgents( void )
{
    Agent* agent = new Agent();
    vec3 position(100, 100, 0);
    agent->SetPosition( position );
    vec3 speed(100, 100, 0);
    agent->SetSpeed( speed );
    agent->SetRadius( 30 );
    agent->SetColor(GetRGB(0xff, 0, 0));
    m_agentArray.push_back( agent );

    Agent* agent2 = new Agent();
    vec3 position2(800, 600, 0);
    agent2->SetPosition( position2 );
    vec3 speed2(-100, -100, 0);
    agent2->SetSpeed( speed2 );
    agent2->SetRadius( 40 );
    agent2->SetColor(GetRGB(0, 0xff, 0));
    m_agentArray.push_back( agent2 );

    Agent* agent3 = new Agent();
    vec3 position3(200, 600, 0);
    agent3->SetPosition( position3 );
    vec3 speed3(-200, 200, 0);
    agent3->SetSpeed( speed3 );
    agent3->SetRadius( 20 );
    agent3->SetColor(GetRGB(0, 0, 0xff));
    m_agentArray.push_back( agent3 );


    Agent* agent4 = new Agent();
    vec3 position4(800, 200, 0);
    agent4->SetPosition( position4 );
    vec3 speed4(200, -200, 0);
    agent4->SetSpeed( speed4 );
    agent4->SetRadius( 20 );
    agent4->SetColor(GetRGB(0xff, 0, 0xff));
    m_agentArray.push_back( agent4 );

    Agent* agent5 = new Agent();
    vec3 position5(800, 200, 0);
    agent5->SetPosition( position5 );
    vec3 speed5(-200, -200, 0);
    agent5->SetSpeed( speed5 );
    agent5->SetRadius( 20 );
    agent5->SetColor(GetRGB(0xff, 0xff, 0));
    m_agentArray.push_back( agent5 );

    Agent* agent6 = new Agent();
    vec3 position6(800, 200, 0);
    agent6->SetPosition( position6 );
    vec3 speed6(200, 200, 0);
    agent6->SetSpeed( speed6 );
    agent6->SetRadius( 20 );
    agent6->SetColor(GetRGB(0, 0xff, 0xff));
    m_agentArray.push_back( agent6 );

    Agent* agent7 = new Agent();
    vec3 position7(800, 200, 0);
    agent7->SetPosition( position7 );
    vec3 speed7(-200, 200, 0);
    agent7->SetSpeed( speed7 );
    agent7->SetRadius( 20 );
    agent7->SetColor(GetRGB(0xff, 0xff, 0xff));
    m_agentArray.push_back( agent7 );

    Agent* dumbAgent = new Agent();
    vec3 dumbPosition(600, 400, 0);
    dumbAgent->SetPosition( dumbPosition );
    vec3 dumbSpeed(0, 0, 0);
    dumbAgent->SetSpeed( dumbSpeed );
    dumbAgent->SetRadius( 100 );
    dumbAgent->SetColor(GetRGB(0xff, 0xff, 0xff));
    m_agentArray.push_back( dumbAgent );  
}
